package com.info.zhiduoduo.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.order.OrderListFormBean;
import com.info.zhiduoduo.common.constants.Constants;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.NumberUtil;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.OrderBriefDto;
import com.info.zhiduoduo.repository.entity.company.Company;
import com.info.zhiduoduo.repository.entity.order.Order;
import com.info.zhiduoduo.repository.mapper.company.CompanyProjectMapper;
import com.info.zhiduoduo.repository.mapper.order.OrderMapper;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class OrderService extends BaseService {

	@Autowired
	CompanyProjectMapper companyProjectMapper;
	@Autowired
	OrderMapper orderMapper;

	/**
	 * 开联通支付结果通知
	 *
	 * @param object
	 * @return
	 */
	public String payNotify(Object object) {

		return null;
	}

	/**
	 * 获取订单列表
	 *
	 * @param formBean
	 * @return
	 */
	public SmartResult getOrderList(BaseSimpleFormBean<OrderListFormBean> formBean) {
		OrderListFormBean reqdata = formBean.getReqdata();
		int userid = formBean.getUserid();
		int idtype = formBean.getIdtype();
		if (reqdata == null || NumberUtil.isNotAllPositive(userid, idtype)) return errorParamSmartResult();

		Date startDate = reqdata.getStartDate();
		Date endDate = reqdata.getEndDate();
		String projectname = reqdata.getProjectname();
		String username = reqdata.getUsername();
		String orderno = reqdata.getOrderno();
		String payno = reqdata.getPayno();
		int state = reqdata.getState();

		if (startDate == null) startDate = new Date(0);
		if (endDate == null) endDate = new Date();

		//1:待支付 2:支付中 3:已支付 4:已完成 5:支付失败 6:支付超时 7:订单取消 8:退款中 9:已退款
		List<Integer> stateList = new ArrayList<>();
		if (state == 1) { //支付中
			stateList.addAll(Arrays.asList(Constants.orderstate_1, Constants.orderstate_5, Constants.orderstate_6));
		} else if (state == 2) { //已支付
			stateList.addAll(Arrays.asList(Constants.orderstate_3, Constants.orderstate_4));
		} else if (state == 3) { //待退款
			stateList.add(Constants.orderstate_8);
		} else if (state == 4) { //已退款
			stateList.add(Constants.orderstate_9);
		}
		String stateStr = StringUtils.join(stateList, ",");

		int compid = 0;
		if (idtype == 2) compid = userid;
		Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
		List<OrderBriefDto> orderList = orderMapper.getOrderList(compid, projectname, username, orderno, payno, startDate, endDate,
				stateStr);

		return smartResult(orderList, page.getTotal(), page.getPages(), page.getPageNum());
	}

	/**
	 * 企业付款 项目订单.
	 */
	public SmartResult CompanyPayOrder(BaseSimpleFormBean<Integer> formBean) {

		//付款后,项目开始计时.
		int userid = formBean.getUserid();
		int idtype = formBean.getIdtype();
		Integer orderid = formBean.getReqdata();
		if (idtype != 2 || NumberUtil.isNotAllPositive(orderid, userid)) return errorParamSmartResult();

		Order order = new Order().selectById(orderid);
		Company company = new Company().selectById(userid);

		if (order == null || company == null || !company.getCompid().equals(order.getCompid())) {
			return smartResult(StatusMsg.StatusMsg_101);
		}

		return null;
	}

}
